package com.zhangxin.threadcoreknowledge.stopthread;

/**
 * 每次循环都会调用sleep或者wait等方法
 */
public class Demo03RightWayStopThreadWithSleepEveryLoop {
    public static void main(String[] args) throws InterruptedException {
        Runnable runnable = () -> {
            int num = 0;
                try {
                    while (num < 10000) {
                        if (num % 100 == 0) {
                            System.out.println(num + "是100的倍数");
                        }
                        num++;
                        Thread.sleep(10);  // 对比上一个案例，sleep方法在循环内，这个时候，就不需要自己去判断是否收到中断异常
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
        };
        Thread thread = new Thread(runnable);
        thread.start();
        Thread.sleep(5000);
        thread.interrupt();
    }


}
